﻿using UnityEngine;
using System.Collections;

public class BloodParticle : MonoBehaviour
{
	// Properties

	[SerializeField]
	GameObject			m_particleOriginal;
	[SerializeField]
	int					m_particleCount	= 10;
	[SerializeField]
	float				m_angle			= 75f;
	[SerializeField]
	float				m_minPower		= 1f;
	[SerializeField]
	float				m_maxPower		= 3f;
	[SerializeField]
	float				m_powerYScale	= 2f;			// 파워를 Y축 방향으로 얼만큼 더 세게 할것인가
	[SerializeField]
	float				m_gravityRandom	= 0.2f;

	[SerializeField]
	float				m_startYOffset	= 4f;

	// Members

	Transform			m_localT;


	void Awake()
	{
		m_localT	= transform;

		for(int i = 0; i < m_particleCount; i++)
			CreateParticleAndShoot();

		GameObject.Destroy(gameObject, 5f);
	}

	void CreateParticleAndShoot()
	{
		GameObject newParticle	= Instantiate(m_particleOriginal) as GameObject;
		Transform partT			= newParticle.transform;

		partT.parent			= m_localT;
		partT.localPosition		= Vector3.up * m_startYOffset;

		Vector3 dirVector		= Vector3.up;										// 랜덤 각도 계산
		float randomAngle		= Random.Range(-m_angle / 2f, m_angle / 2f);
		Quaternion rotation		= Quaternion.AngleAxis(randomAngle, Vector3.forward);
		dirVector				= rotation * dirVector;
		dirVector.y				*= m_powerYScale;

		float randomPower		= Random.Range(m_minPower, m_maxPower);				// 랜덤 파워 계산

		Vector2 finalPower		= randomPower * dirVector;
		newParticle.rigidbody2D.AddForce(new Vector2(finalPower.x, finalPower.y));
		newParticle.rigidbody2D.gravityScale *= 1 + Random.Range(-m_gravityRandom, m_gravityRandom) / 2f;
	}
}
